package edu.scau.mis.rest.service.impl;

import edu.scau.mis.rest.entity.RestCategory;
import edu.scau.mis.rest.entity.vo.RestCategoryVo;
import edu.scau.mis.rest.entity.vo.RestItemVo;
import edu.scau.mis.rest.mapper.RestCategoryMapper;
import edu.scau.mis.rest.service.RestCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RestCategory)表服务实现类
 *
 * @author makejava
 * @since 2022-03-10 00:39:27
 */
@Service("restCategoryService")
public class RestCategoryServiceImpl implements RestCategoryService {
    @Resource
    private RestCategoryMapper restCategoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RestCategory queryById(Long id) {
        return this.restCategoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param restCategory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RestCategory> queryByPage(RestCategory restCategory, PageRequest pageRequest) {
        long total = this.restCategoryMapper.count(restCategory);
        return new PageImpl<>(this.restCategoryMapper.queryAllByLimit(restCategory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param restCategory 实例对象
     * @return 实例对象
     */
    @Override
    public RestCategory insert(RestCategory restCategory) {
        this.restCategoryMapper.insert(restCategory);
        return restCategory;
    }

    /**
     * 修改数据
     *
     * @param restCategory 实例对象
     * @return 实例对象
     */
    @Override
    public RestCategory update(RestCategory restCategory) {
        this.restCategoryMapper.update(restCategory);
        return this.queryById(restCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.restCategoryMapper.deleteById(id) > 0;
    }

    /**
     * 全查询
     *
     * @return
     */
    @Override
    public List<RestCategory> queryAll(){return this.restCategoryMapper.queryAll();}

    /**
     * 产品与产品种类全查询
     *
     * @return
     */
    public List<RestCategoryVo> queryAllCategoryAndItem(){
        List<RestCategoryVo> restCategoryVoList = this.restCategoryMapper.queryAllCategoryAndItem();
        //设置key，方便小程序检索
        for(int i = 0; i < restCategoryVoList.size(); i++){
            restCategoryVoList.get(i).setKey(i + 1);
            List<RestItemVo> restItemVoList = restCategoryVoList.get(i).getChild();
            for(int j = 0; j < restItemVoList.size(); j++){
                restItemVoList.get(j).setParentKey(i + 1);
                restItemVoList.get(j).setKey(j);
                //最后一个的load改成finish，别问有什么用，我也不知道
                if(j == restItemVoList.size() - 1){
                    restItemVoList.get(j).setLoad("finish");
                }
            }
        }

        return restCategoryVoList;
    }

    /**
     * 通过类别证号查询类别
     *
     * @param categoryNo
     * @return
     */
    @Override
    public RestCategory queryCategoryByCategotyNo(String categoryNo){return this.restCategoryMapper.queryCategoryByCategotyNo(categoryNo);}
}
